<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div>
        <input type="button" value="重新开始一局游戏" onclick="startNewGame()">
    </div>
    <div>请输入要猜的数字：
        <input type="text" id="inputNum">
        <input type="button" value="猜" onclick="checkNumber()">
    </div>
    <div>已经猜测的次数：
        <label id="showTimes">0次</label>
    </div>
    <div>结果：
        <label id="showResult"></label>
    </div>
</body>
<script>
    let target= Math.floor(Math.random() * 100);//随机数
    let times=0;//猜的次数
    let inputNum=document.getElementById("inputNum");
    let showTimes= document.getElementById("showTimes");
    let showResult = document.getElementById("showResult");

    function startNewGame(){
        target = Math.floor(Math.random() * 100);//重置随机数
        times=0;//重置猜测次数
        inputNum.value="";//清空输入框
        showTimes.innerText=times+"次";//重置显示
        showResult.innerText="";//重置显示
    }

    function checkNumber(){
        times++;
        showTimes.innerText = times + "次";

        let num=parseInt(inputNum.value);//输入的次数
        if(num<target)
            showResult.innerText="小了";
        else if(num>target)
            showResult.innerText = "大了";
        else
            showResult.innerText="猜对了";
        
    }

</script>
</html>